"use strict"


// let myNetwork = { chainId:137, chainName:"Polygon Mainnet", suisse:{ symbol:"MATIC" } , decimals:18 , rpcUrl:"https://polygon-mainnet.infura.io" ,rpcUrl2:"https://polygon.llamarpc.com" , scanUrl:"https://polygonscan.com"  }
// let myNetwork = { chainId:42161, chainName:"Arbitrum One", suisse:{ symbol:"ETH" } , decimals:18 , rpcUrl:"https://1rpc.io/arb" ,rpcUrl2:"https://arbitrum.meowrpc.com" , scanUrl:"https://explorer.arbitrum.io"  }
let myNetwork = {
    chainId:10, chainName:"OP Mainnet",
    suisse:{ symbol:"ETH" } , decimals:18 ,
    rpcUrl:"https://mainnet.optimism.io" ,
    rpcUrl2:"https://mainnet.optimism.io" ,
    scanUrl:"https://optimistic.etherscan.io"
}

const Contract_VOOK = "0x5972D5Ce8dAfE2F54c18AF8069F1Eff25bc947e5";
const perMintCount = 0.0006;

let Contract_VOOK_Abi = [ { "inputs": [ { "internalType": "uint256", "name": "_txIndex", "type": "uint256" } ], "name": "confirmTransaction", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address[]", "name": "_owners", "type": "address[]" }, { "internalType": "uint256", "name": "_numConfirmationsRequired", "type": "uint256" } ], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [], "name": "EnforcedPause", "type": "error" }, { "inputs": [], "name": "ExpectedPause", "type": "error" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": true, "internalType": "uint256", "name": "txIndex", "type": "uint256" } ], "name": "ConfirmTransaction", "type": "event" }, { "inputs": [ { "internalType": "uint256", "name": "_txIndex", "type": "uint256" } ], "name": "executeTransaction", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": true, "internalType": "uint256", "name": "txIndex", "type": "uint256" } ], "name": "ExecuteTransaction", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "_oldOwner", "type": "address" }, { "internalType": "address", "name": "_newOwner", "type": "address" } ], "name": "multiSigSetOwner", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "pause", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "address", "name": "account", "type": "address" } ], "name": "Paused", "type": "event" }, { "inputs": [ { "internalType": "uint256", "name": "_txIndex", "type": "uint256" } ], "name": "revokeConfirmation", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": true, "internalType": "uint256", "name": "txIndex", "type": "uint256" } ], "name": "RevokeConfirmation", "type": "event" }, { "inputs": [ { "internalType": "string", "name": "inscription", "type": "string" } ], "name": "setInscription_mint", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "string", "name": "inscription", "type": "string" } ], "name": "setInscription_transfer", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "_to", "type": "address" }, { "internalType": "uint256", "name": "_value", "type": "uint256" }, { "internalType": "bytes", "name": "_data", "type": "bytes" } ], "name": "submitTransaction", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "owner", "type": "address" }, { "indexed": true, "internalType": "uint256", "name": "txIndex", "type": "uint256" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "value", "type": "uint256" }, { "indexed": false, "internalType": "bytes", "name": "data", "type": "bytes" } ], "name": "SubmitTransaction", "type": "event" }, { "inputs": [], "name": "unpause", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "address", "name": "account", "type": "address" } ], "name": "Unpaused", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "string", "name": "content", "type": "string" } ], "name": "ethrunes_protocol_Inscribe", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "transfer", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "recipient", "type": "address" } ], "name": "withdraw", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "stateMutability": "payable", "type": "receive" }, { "inputs": [], "name": "_inscription_mint", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "_inscription_transfer", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "counter", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "FEE", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" }, { "internalType": "address", "name": "", "type": "address" } ], "name": "isConfirmed", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "", "type": "address" } ], "name": "isOwner", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "MAX_COUNT", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "multiSigTranLen", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "numConfirmationsRequired", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "name": "owners", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "paused", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "string", "name": "_a", "type": "string" }, { "internalType": "string", "name": "_b", "type": "string" } ], "name": "strConcat", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "stateMutability": "pure", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "name": "transactions", "outputs": [ { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "value", "type": "uint256" }, { "internalType": "bytes", "name": "data", "type": "bytes" }, { "internalType": "bool", "name": "executed", "type": "bool" }, { "internalType": "uint256", "name": "numConfirmations", "type": "uint256" }, { "internalType": "uint256", "name": "locktime", "type": "uint256" } ], "stateMutability": "view", "type": "function" } ]


let project_tick = "opk";

function GetContractVOOK()
{
    return new window._ethers.Contract(Contract_VOOK, Contract_VOOK_Abi, Helper.provider.getSigner());
}

async function doMint(count)
{
    await toSwitchNetwork();

    let network = await Helper.provider.getNetwork();
    if(network.chainId==myNetwork.chainId)
    {
        var signer = Helper.provider.getSigner();

        try{
            var tx = await signer.sendTransaction({
                to: Contract_VOOK,
                value: window._ethers.utils.parseUnits( (perMintCount * count).toString()),
            });

            var receipt = await tx.wait();

            console.log(receipt);
            ShowToastr("success","mint");
        }catch(e){
            console.log(e);
            if(e.data.message.indexOf("insufficient funds for gas")){
                ShowToastrError("transfer error","insufficient funds for gas", e);
            }
        }
    }
    else {
        ShowToastrError("transfer error","Please switch the wallet network to ", myNetwork.chainName);
    }
}

async function doTransfer()
{
    var addressTo = $(".transfer_address").val();

    await toSwitchNetwork();

    let network = await Helper.provider.getNetwork();
    if(network.chainId==myNetwork.chainId)
    {
        try{
            const contract = new window._ethers.Contract(Contract_VOOK, Contract_VOOK_Abi, Helper.provider.getSigner());
            var rel1  = await contract.transfer(addressTo,"1000");
            console.log(rel1);
            ShowToastr("success","transfer send");
        }catch(e){
            console.log(e);
            if(e.data.message.indexOf("insufficient funds for gas")){
                ShowToastrError("transfer error","insufficient funds for gas", e);
            }
        }
    }
    else {
        ShowToastrError("transfer error","Please switch the wallet network to ", myNetwork.chainName);
    }
}


async function addChain() {
    try {
        // infoChain 获取要添加的链的 {chainId，chainName，nativeCurrency，rpcUrls，blockExplorerUrls}
        if (1) {
            await window[sessionStorage.getItem('ethereumType') || 'ethereum'].request({
                method: "wallet_addEthereumChain",
                params: [{
                    chainId: '0x' + myNetwork.chainId.toString(16),
                    chainName: myNetwork.chainName,
                    nativeCurrency: {
                        name: myNetwork.suisse.symbol,
                        symbol: myNetwork.suisse.symbol,
                        decimals: myNetwork.decimals,
                    },
                    rpcUrls: [myNetwork.rpcUrl],
                    blockExplorerUrls: [myNetwork.scanUrl],
                }],
            });
        }
    } catch (error) {
        console.log(error)
        throw error
    }
}

async function toSwitchNetwork()
{
    let network = await Helper.provider.getNetwork();
    if(network==undefined||network.chainId!=myNetwork.chainId)
    {
      try {
        await window.ethereum.request({
            method: 'wallet_switchEthereumChain',
            params: [{ chainId: '0x'+ (myNetwork.chainId).toString(16) }],
        });
        window.location.href = "javascript:location.reload();";
      } catch (error) {
        if (error.code === 4902) {
            addChain()
        }
        return;
      }
    }
}

async function GetVOOKProgress()
{
    const contract = new window._ethers.Contract(Contract_VOOK, Contract_VOOK_Abi, window._ethers.getDefaultProvider(myNetwork.rpcUrl2));

    var rel1  = await contract.counter();

    var rel2  = await contract.MAX_COUNT();

    var value = Helper.Fix2( Helper.Div( rel1.toString(), rel2.toString() ) );
    // value = Helper.Add(value,"2.05")

    return new Promise((resolve, reject) => {
        resolve( value );
    })
}


(function($) {
    "use strict"




})(jQuery);


